package cn.com.klun.tjy.factory;

import cn.com.klun.tjy.cache.InitApplicationCache;
import cn.com.klun.tjy.dao.TaskInfoDAO;
import cn.com.klun.tjy.dao.entity.TblTaskBasicConfig;
import cn.com.klun.tjy.service.TimerBuildService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;

@Service
public class MainTaskCreateFactory {
    private Logger logger = LoggerFactory.getLogger(this.getClass().getSimpleName());

    @Resource
    private TimerBuildService childTimerBuildServiceImpl;

    @Autowired
    private TaskInfoDAO taskInfoDAO;

    @PostConstruct
    public void initTask() {
        logger.info("执行任务定时器初始化工厂开始执行......");
        try{
            //读取站点推送配置到缓存
            //List<TblTaskBasicConfig> taskConfigInfo = InitApplicationCache.getTaskConfigInfo();
            //InitApplicationCache.setTaskConfigInfo(taskInfoDAO.getTaskBasicConfig());

            List<TblTaskBasicConfig> list = childTimerBuildServiceImpl.getCreateTaskInfo();
            InitApplicationCache.setTaskConfigInfo(list);
            for(TblTaskBasicConfig config : list){
                childTimerBuildServiceImpl.createTimer(config);
            }
            logger.info("执行任务定时器初始化工厂执行结束，共有"+list.size()+"个定时器被初始化。");
        }catch(Exception e){
            logger.error("执行任务定时器初始化工厂异常:",e);
        }
    }
}
